#include "FunctionContext.h"
#include "ScriptCompiler/Description/FunctionBodyDescription.h"
#include "StatementContext.h"
#include "ScriptCompiler/Description/ParameterDescription.h"

CFunctionContext::CFunctionContext(
    std::shared_ptr<CFunctionBodyDescription> InFunctionBody
    ,const SDecoratedTypeDescription & InReturnType
    ,const TVector<std::shared_ptr<CParameterDescription>> & InParameters 
    ,std::shared_ptr<CScriptFileContext> InFileContext
    ,std::shared_ptr<CTypeDescription> InOwnerClass
    )
{
    FunctionBody = InFunctionBody;
    ReturnType = InReturnType;
    FileContext = InFileContext;
    Parameters = InParameters;
    OwnerClass = InOwnerClass;
}

std::shared_ptr<CStatementContext> CFunctionContext::NewSentence(
    std::shared_ptr<CStatementContext> InParentSentence
    ,EStatementType InStatementType
    )
{
    auto NewSentence = std::make_shared<CStatementContext>(
        shared_from_this()
        ,InParentSentence
        ,InStatementType
        );
    if(InParentSentence)
    {
        InParentSentence->ChildSentences.Add(NewSentence);
    }

    return NewSentence;
}



std::shared_ptr<CParameterDescription> CFunctionContext::FindParameterByName(const CString &InParameterName)
{
    for (auto& Parameter : Parameters)
    {
        if (Parameter->Name == InParameterName)
        {
            return Parameter;
        }
    }

    return nullptr;
}

std::shared_ptr<CTypeDescription> CFunctionContext::GetOwnerClass()
{
    return OwnerClass.lock();
}


